package com.leil.movies.project.rest;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.leil.movies.project.rest.exception.DouBanApiException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

@Component
public class RestUtils {


    @Autowired
    RestTemplate restTemplate;

    /**
     * 获取json对象
     * @param url
     * @return
     */
    public JSONObject doGet(String url) {
        ResponseEntity<JSONObject> response = this.restTemplate.getForEntity(url, JSONObject.class);
        JSONObject json = response.getBody();
        json.put("code", response.getStatusCode().value());
        return json;
    }

    /**
     * 获取json数组对象
     * @param url
     * @return
     */
    public JSONObject doGetJsonArr(String url) {
        ResponseEntity<JSONArray> response = this.restTemplate.getForEntity(url, JSONArray.class);
        JSONArray data = response.getBody();
        JSONObject json = new JSONObject();
        json.put("code", response.getStatusCode().value());
        json.put("data", data);
        return json;
    }

}
